home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD-ROM Today - The Disc! 5
/
CD-ROM Today - The Disc (Issue 5)(November 1994).ISO
/
mac
/
Mac shareware
/
Education
/
RLaB
/
examples
/
least_squares.r
< prev
next >
Wrap
Text File
|
1994-09-21
|
1KB
|
64 lines
//-------------------------------------------------------------------//
//
// A Simple Least Squares Data Fitting Example
//
//
// Set the random number generator to uniform distribution, with
// lower bound = -2, and upper bound = 5.
//
rand("uniform",-2, 5);
// Generate random data with a linearly varying component. The
// linearly varying component is formed, and stored in `off'. The
// simulated, measured data is stored in `b'.
//
off = 0:-20:-.2;
b = ((off + 22) + rand( size(off) ));
//
// Generate the Data matrix, A.
//
// | 1 t t^2 |
// A = | 1 t t^2 |
// | 1 t t^2 |
// . . .
// . . .
// . . .
//
m = b.n;
t = (1:m)/m;
A = [ ones(m,1), t', (t.^2)' ];
//
// Now use left division (least squares) to solve for `x'.
//
x = A\b';
//
// Create a simple function that uses the computed parameters to
// make predictions.
//
ls = function(t)
{
return x[1] + x[2]*t + x[3]*t.^2;
}
//
// Plot a comparison of the original data, and the computed
// values.
//
plgrid ();
ptitle ( "RLaB Least Squares Example" );
xlabel ( "Independent Variable" );
ylabel ( "Dependent Variable" );
plot( [ t; b; ls( t ) ]' );